package controler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import view.ContactsPanel;
import view.Global;
import view.GroupsPanel;
import view.FormPanel;
import model.AddressBook;
import model.Contact;
import model.Group;

/**Controleur des actions sur les boutons
 * 
 *
 */
public class AddressBookControler implements ActionListener {

	private AddressBook addresBook;
	private Global glb;
	private ContactsPanel vueC;
	
	public AddressBookControler(Global glb) {
		// TODO Auto-generated constructor stub
		this.addresBook = AddressBook.getInstance();
		this.glb= glb;
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		
		//CREATION D'UN CONTACT
		if (e.getActionCommand().equals("newContact")){
			Group g  = (Group) glb.getGroupPanel().getLstGroup().getSelectedValue();
			if(g!=null){
				Contact c = new Contact(view.ContactsPanel.getNewContactName().getText());
				if(view.ContactsPanel.getNewContactName().getText().length()!=0){
					g.addContact(c);
					glb.getContactsPanel().majListofContact(g);
				}else{
					javax.swing.JOptionPane.showMessageDialog(null,"Contact name field is empty"); 
				}
			}else{
				javax.swing.JOptionPane.showMessageDialog(null,"Please chose a group name"); 
			}
		}
		
		//SAUVEGARDE CONTACT
		if (e.getActionCommand().equals("saveContact")){
			Group g  = (Group) glb.getGroupPanel().getLstGroup().getSelectedValue();
			Contact c  = (Contact) glb.getContactsPanel().getLstContact().getSelectedValue();
			if(c!=null){
				c.setFirstname(glb.getFormPanel().getFirstName().getText());
				c.setLastname(glb.getFormPanel().getLastName().getText());
				c.setHomeAdress(glb.getFormPanel().getHomeAddress().getText());
				c.setCompanyAdress(glb.getFormPanel().getCompanyAddress().getText());
				c.setPersonalMobilePhone(glb.getFormPanel().getPersonalMobilePhone().getText());
				c.setPersonalHomePhone(glb.getFormPanel().getPersonalHomePhone().getText());
				c.setProfessionalMobilePhone(glb.getFormPanel().getProfessionalMobilePhone().getText());
				c.setProfessionalHomePhone(glb.getFormPanel().getProfessionalHomePhone().getText());
				c.setHomePage(glb.getFormPanel().getHomePage().getText());
				c.setProfessionalEmail(glb.getFormPanel().getProfessionalEmail().getText());
				c.setPersonalEmail(glb.getFormPanel().getPersonalEmail().getText());
				c.setPhoto(glb.getFormPanel().getPhoto().getText());
				c.setFacebookAccount(glb.getFormPanel().getFacebookAccount().getText());
				c.setTwitterAccount(glb.getFormPanel().getTwitterAccount().getText());
			}
			glb.getContactsPanel().majListofContact(g);	
		}
		
		/*//EDITION D'UN CONTACT
		if (e.getActionCommand().equals("editContact")){
			
		}*/
		//SUPPRESSION D'UN CONTACT
		if (e.getActionCommand().equals("deleteContact")){
			Group g  = (Group) glb.getGroupPanel().getLstGroup().getSelectedValue();
			Contact c  = (Contact) glb.getContactsPanel().getLstContact().getSelectedValue();
			if(g!=null && c!=null){
				g.deleteContact(c);
				glb.getContactsPanel().majListofContact(g);
			}
			glb.getFormPanel().blankJTexte();
		}

		// CREATION D'UN GROUPE
		if (e.getActionCommand().equals("newGroup")){
			
			AddressBook myBook = AddressBook.getInstance();
			if(view.GroupsPanel.getNewGroupName().getText().length()!=0){
				myBook.addGroup(new Group(view.GroupsPanel.getNewGroupName().getText()));
				glb.getGroupPanel().majListofGroup();	
			}else{
				javax.swing.JOptionPane.showMessageDialog(null,"Group name field is empty"); 
			}
		}
		
		//SUPPRESSION D'UN GROUPE
		if(e.getActionCommand().equals("deleteGroup")){
			AddressBook myBook = AddressBook.getInstance();
			myBook.removeGroup((Group) glb.getGroupPanel().getLstGroup().getSelectedValue());
			glb.getGroupPanel().majListofGroup();
		}
	}
}
